//
//  PopoverAnimator.swift
//  swiftWeiBoDemo
//
//  Created by zzw on 16/5/6.
//  Copyright © 2016年 zzw. All rights reserved.
//

import UIKit

let ZZWPopoverAnimatorWillShow = "ZZWPopoverAnimatorWillShow"
let ZZWPopoverAnimatorWilldismiss = "ZZWPopoverAnimatorWilldismiss"



class PopoverAnimator: NSObject ,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{

    var isPresent = false
    
    var presentFrame = CGRectZero
    
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let popover = PopoverPresentationController(presentedViewController: presented,presentingViewController: presenting)
        
        popover.presentFrame = presentFrame
     
        return popover
        
    }
    
    // MARK: - 只要实现了一下方法, 那么系统自带的默认动画就没有了, "所有"东西都需要程序员自己来实现
    /**
     告诉系统谁来负责Modal的展现动画
     
     :param: presented  被展现视图
     :param: presenting 发起的视图
     :returns: 谁来负责
     */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        
        NSNotificationCenter.defaultCenter().postNotificationName(ZZWPopoverAnimatorWillShow, object: self)
 
        return self
    }
    /**
     告诉系统谁来负责Modal的消失动画
     
     :param: dismissed 被关闭的视图
     
     :returns: 谁来负责
     */
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        
        NSNotificationCenter.defaultCenter().postNotificationName(ZZWPopoverAnimatorWilldismiss, object: self)
        
        return self
    }
    
    // MARK: - UIViewControllerAnimatedTransitioning
    /**
     返回动画时长
     
     :param: transitionContext 上下文, 里面保存了动画需要的所有参数
     
     :returns: 动画时长
     */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
    
        return 0.5
    
    }
    
    /**
     告诉系统如何动画, 无论是展现还是消失都会调用这个方法
     
     :param: transitionContext 上下文, 里面保存了动画需要的所有参数
     */
   
     func animateTransition(transitionContext: UIViewControllerContextTransitioning)
     {
    
        if isPresent {
            
            
            let toV = transitionContext.viewForKey(UITransitionContextToViewKey)
            
                toV?.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            
                transitionContext.containerView()?.addSubview(toV!)
            
            toV?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 
              
                ()-> Void in toV?.transform = CGAffineTransformIdentity
                
            }){
                (_) -> Void in transitionContext.completeTransition(true)
            }
            
        }else{
            
            let fromV = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 
                () -> Void in fromV?.transform = CGAffineTransformMakeScale(1.0, 0.000000001)
            }){
            
                (_) -> Void in transitionContext.completeTransition(true)
            }
            
        
        
        }
    
    }
    
    
}
